<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="资产编码" prop="assetNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetNo"-->
<!--          placeholder="请输入资产编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="资产类别id" prop="classId">-->
<!--        <el-input-->
<!--          v-model="queryParams.classId"-->
<!--          placeholder="请输入资产类别id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="资产类别" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入资产类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="资产类别编码" prop="classNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.classNo"-->
<!--          placeholder="请输入资产类别编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="品牌名" prop="assetBrand">
        <el-input
          v-model="queryParams.assetBrand"
          placeholder="请输入品牌名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="assetModel">
        <el-input
          v-model="queryParams.assetModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="计量单位" prop="assetUnit">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetUnit"-->
<!--          placeholder="请输入计量单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="资产单价" prop="assetPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetPrice"-->
<!--          placeholder="请输入资产单价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="存放地点" prop="assetLocation">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetLocation"-->
<!--          placeholder="请输入存放地点"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="资产数量" prop="assetQuantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetQuantity"-->
<!--          placeholder="请输入资产数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="可用数量" prop="assetAvailQuantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.assetAvailQuantity"-->
<!--          placeholder="请输入可用数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:AssetDetails:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:AssetDetails:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:AssetDetails:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:AssetDetails:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AssetDetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="自增id" align="center" prop="id" />-->
      <el-table-column label="资产名称" align="center" prop="assetName" />
<!--      <el-table-column label="资产编码" align="center" prop="assetNo" />-->
<!--      <el-table-column label="资产类别id" align="center" prop="classId" />-->
      <el-table-column label="资产类别名称" align="center" prop="className" />
<!--      <el-table-column label="资产类别编码" align="center" prop="classNo" />-->
      <el-table-column label="品牌名" align="center" prop="assetBrand" />
      <el-table-column label="规格型号" align="center" prop="assetModel" />
      <el-table-column label="计量单位" align="center" prop="assetUnit" />
      <el-table-column label="存放地点" align="center" prop="assetLocation" />
      <el-table-column label="资产单价" align="center" prop="assetPrice" />
      <el-table-column label="资产数量" align="center" prop="assetQuantity" />
      <el-table-column label="可用数量" align="center" prop="assetAvailQuantity" />
<!--      <el-table-column label="当前状态" align="center" prop="assetStatus" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:AssetDetails:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:AssetDetails:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产名称" prop="assetName">
          <el-input v-model="form.assetName" placeholder="请输入资产名称" />
        </el-form-item>
<!--        <el-form-item label="资产编码" prop="assetNo">-->
<!--          <el-input v-model="form.assetNo" placeholder="请输入资产编码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="资产类别id" prop="classId">-->
<!--          <el-input v-model="form.classId" placeholder="请输入资产类别id" />-->
<!--        </el-form-item>-->


<!--        <el-form-item label="资产类别" prop="className">-->
<!--          <el-input v-model="form.className" placeholder="请输入资产类别名称" />-->
<!--        </el-form-item>-->

        <el-form-item label="资产类别" prop="className">
          <el-select v-model="form.className" placeholder="资产类别">
            <el-option
              v-for="item in classNameOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>


<!--        <el-form-item label="资产类别编码" prop="classNo">-->
<!--          <el-input v-model="form.classNo" placeholder="请输入资产类别编码" />-->
<!--        </el-form-item>-->
        <el-form-item label="品牌名" prop="assetBrand">
          <el-input v-model="form.assetBrand" placeholder="请输入品牌名" />
        </el-form-item>
        <el-form-item label="规格型号" prop="assetModel">
          <el-input v-model="form.assetModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="计量单位" prop="assetUnit">
          <el-input v-model="form.assetUnit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="资产单价" prop="assetPrice">
          <el-input v-model="form.assetPrice" placeholder="请输入资产单价" />
        </el-form-item>
        <el-form-item label="资产数量" prop="assetQuantity">
          <el-input v-model="form.assetQuantity" placeholder="请输入资产数量" />
        </el-form-item>
        <el-form-item label="存放地点" prop="assetLocation">
          <el-input v-model="form.assetLocation" placeholder="请输入存放地点" />
        </el-form-item>
<!--        <el-form-item label="可用数量" prop="assetAvailQuantity">-->
<!--          <el-input v-model="form.assetAvailQuantity" placeholder="请输入可用数量" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标志" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssetDetails, getAssetDetails, delAssetDetails, addAssetDetails, updateAssetDetails } from "@/api/module/AssetDetails";
import { getDicts } from '@/api/system/dict/data';
export default {
  name: "AssetDetails",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产明细表格数据
      AssetDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetName: null,
        assetNo: null,
        classId: null,
        className: null,
        classNo: null,
        assetBrand: null,
        assetModel: null,
        assetUnit: null,
        assetPrice: null,
        assetLocation: null,
        assetQuantity: null,
        assetAvailQuantity: null,
        assetStatus: null,
      },
      classNameOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAsssetClass();
  },
  methods: {
    /** 查询资产明细列表 */
    getList() {
      this.loading = true;
      listAssetDetails(this.queryParams).then(response => {
        this.AssetDetailsList = response.rows;
        // 资产类型判断
        let arr = this.AssetDetailsList;
        arr.forEach(item => {
          item.className = this.getValue(this.classNameOptions, item.className)
        });
        this.AssetDetailsList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },
    // 获取资产类别
    getAsssetClass() {
      getDicts('asset_class').then(response => {
        this.classNameOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetName: null,
        assetNo: null,
        classId: null,
        className: null,
        classNo: null,
        assetBrand: null,
        assetModel: null,
        assetUnit: null,
        assetPrice: null,
        assetLocation: null,
        assetQuantity: null,
        assetAvailQuantity: null,
        assetStatus: "0",
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssetDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssetDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssetDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产明细编号为"' + ids + '"的数据项？').then(function() {
        return delAssetDetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/AssetDetails/export', {
        ...this.queryParams
      }, `AssetDetails_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
